#cs
	Autoit Test de mmo en tile
    Copyright (C) 2010 Jones

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>
#ce

#include-once

Dim $monster[1][11]
Global $monster_selected
$monster_popup_open = 0

Func add_monster($id = 1)
	$succes_monster = 0

	Global $monster_size = UBound($monster)

	ReDim $monster[$monster_size+1][11]


	$monster[$monster_size-1][0] = 1 ; ID du monstre
	$monster[$monster_size-1][1] = "Monstre 1" ; Nom
	$monster[$monster_size-1][2] = 0 ; Skin
	$monster[$monster_size-1][3] = 0 ; orientation
	$monster[$monster_size-1][4] = 0 ; Position X
	$monster[$monster_size-1][5] = 0 ; Position Y
	$monster[$monster_size-1][6] = 1 ; Level
	$monster[$monster_size-1][7] = 10 ; Vie
	$monster[$monster_size-1][8] = 5 ; Attaque
	$monster[$monster_size-1][9] = 5 ; Defense
	$monster[$monster_size-1][10] = 0 ; Handle widget

;~ 	Placement de monstre intelligent
$x = Random(0,$widht_event-1,1)
$y = Random(0,$height_event-1,1)

Do
If $event[$y][$x] = 0 Then
	$x = Random(0,$widht_event-1,1)
	$y = Random(0,$height_event-1,1)

	$monster_x = _ArrayFindAll($monster,$x,0,0,0,0,2)
	$size_monster_x = UBound($monster_x)

	For $i = 0 To $size_monster_x-1 Step 1
		If $monster[$monster_x[$i]][4] <> $x And $monster[$monster_x[$i]][5] <> $y Then
		$x = Random(0,$widht_event-1,1)
		$y = Random(0,$height_event-1,1)
		EndIf
	Next
Else
	$succes_monster = 1
EndIf
Until $succes_monster = 1

	$monster[$monster_size-1][4] = $x ; Position X
	$monster[$monster_size-1][5] = $y ; Position Y

	$monster[$monster_size-1][2] = _IrrGetTexture("ressources/textures/mobs/" & $monster[$monster_size-1][0] & ".png")

$monster[$monster_size-1][10] = _IrrAddImage($monster[$monster_size-1][2],$monster[$monster_size-1][4]*40,$monster[$monster_size-1][5]*40,$IRR_USE_ALPHA,1)
ConsoleWrite(time() & "hWidget : " & $monster[$monster_size-1][10] & @CRLF)
;~ delete_monster($monster[$monster_size-1][8])
EndFunc

Func delete_monster($id)
	_IrrGUIRemove($id)
EndFunc

Func atk_monster($id)
	If $monster_popup_open = 1 Then GUIDelete()
	$monster_popup = GUICreate("",200,30,@DesktopWidth*(20/100), @DesktopHeight*(50/100),$WS_POPUP)
	$parent = WinGetHandle($title)
	_SetParent($monster_popup,$parent)
	GUICtrlCreateLabel($monster[$id][1],30,10)
	GUISetState()
	$monster_popup_open = 1
EndFunc

Func get_selected_monster($mouse_x,$mouse_y)
	$monster_size = UBound($monster)
	$hdl = 0
	Dim $return[2]
	$return[0] = -1
	$return[1] = -1

	For $i = 0 To $monster_size-1 Step 1
		$x_pos = $monster[$i][4]
		$x_end = ($x_pos * 40) + 40
		$y_pos = $monster[$i][5]
		$y_end = ($y_pos * 40) + 40

		$win_pos = WinGetPos($title)

		$final_pos_x = ($mouse_x - $win_pos[0]) - $x_end
		If $final_pos_x <= 0 And $final_pos_x >= -40 Then
			$hdl_x = 1
		Else
			$hdl_x = 0
		EndIf

		$final_pos_y = ($mouse_y - $win_pos[1]) - $y_end
		If $final_pos_y <= 40 And $final_pos_y >= 0 Then
			$hdl_y = 1
		Else
			$hdl_y = 0
		EndIf

		$hdl = $hdl_x + $hdl_y

		If $hdl = 2 Then
			$return[0] = $hdl
			$return[1] = get_handle_monster($x_pos,$y_pos)
			$monster_selected = $return[1]
			ExitLoop
		EndIf
	Next

	Return $return
EndFunc

Func get_handle_monster($pos_x,$pos_y)
	$return = -1

	For $i = 0 To UBound($monster)-1
		If $monster[$i][4] = $pos_x And $monster[$i][5] = $pos_y Then
			$return = $i
			ExitLoop
		EndIf
	Next

	Return $return
EndFunc

Func free_monster()
	Dim $monster [1][1]
	Global $monster_size = 0
EndFunc